package com.handydata.antifraud.rule.funcs;

import com.handydata.antifraud.to.RiskInputTO;
import com.handydata.antifraud.to.RiskMiddleTO;
import com.handydata.antifraud.to.RiskOutputTO;
import com.handydata.antifraud.util.ExceptionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 计算反欺诈风险结果info3
 */
@Slf4j
@Component
public class CalcRiskOutputInfo3 implements Calculate {
    @Override
    public void doCalc(RiskInputTO riskInputTO, RiskMiddleTO riskMiddleTO, RiskOutputTO riskOutputTO) {
        List<Integer> list = new ArrayList<>();

        log.info("start calc CalcRiskOutputInfo3 timesRecently60Months{},factInsurePay {}，" +
                "getEndowmentAmount{} insuranceType {} endowmentAmount",
                riskInputTO.getTimesRecently60Months(), riskInputTO.getFactInsurePay(),
                riskInputTO.getEndowmentAmount(), riskInputTO.getInsuranceType(),
                riskInputTO.getEndowmentAmount()

        );
        calcByItem(riskInputTO.getTimesRecently60Months(), "错误：最近60个月累计参保次数{}不是数字", 48, list);
        calcByItem(riskInputTO.getFactInsurePay(), "错误：月薪{}不是数字", 12000, list);
        calcByItem(riskInputTO.getEndowmentAmount(), "错误：养老保险专项余额{}不是数字", 10000, list);

        if("3".equals(riskInputTO.getInsuranceType())) {
            calcByItem(riskInputTO.getEndowmentAmount(), "错误：住房公积金专户余额{}不是数字", 10000, list);
        }

        calcByItem(riskInputTO.getSeriesTimesRecently60Months(), "错误：60个月内最近连续参保次数{}不是数字", 12, list);

        riskOutputTO.setInfo3(String.valueOf(list.size()));
    }

    private void calcByItem(Object item, String itemText, int condition, List<Integer> list) {

        Optional<Object> itemOptional = Optional.ofNullable(item);
        itemOptional.ifPresent(t -> {
            double v = ExceptionUtil.of(() -> Double.valueOf(t.toString()), ex -> log.error(itemText, t)).get();
            if(v > condition) list.add(1);
        });
    }
}
